Page({
  data: {
    keyword:'',
    keywordlist:[],
    status:1,
    //搜索结果相关
    sortKind: "",
    paixu: true,
    goodsStart: 1,
    rows: 20,
    end: 999,
    scrollTop: 1,
    endFlage: 1,
    addflage: 1,
    //商品
    goodsList:[]
  },
  onLoad: function (options) {
    this.getHotWord();
    this.getHistory();
    this.setSorting();
  },
  onShow: function () {
  
  },
  //获得搜索热词
  getHotWord(){
    var url = getApp().data.url + 'sysinfo/getSearchKeyword';
    var body = {};
    getApp().wxAjax(this.splitMenu, url, body);
  },
  splitMenu(res) {
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
     console.log(res)
     this.setData({
       hotlist: resObj.body.hot_words
     })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  //清空关键字
  clearKeyWord(){
    this.setData({
      keyword:'',
      status: 1
    })
    this.setData({
      goodsStart: 1,
      endFlage: 1,
      end: 999,
      goodsList: []
    })
  },
  //清除历史记录
  clearhis(){
    var me = this;
    wx.showModal({
      title: '',
      cancelText:'再想想',
      confirmText:'删除',
      content: '是否清空搜索历史',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.removeStorageSync('keywordlist');
          wx.setStorageSync('keywordlist', [])
          me.setData({
            keywordlist: []
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //获得历史搜索
  getHistory(){
    var keywordlist = wx.getStorageSync('keywordlist');
    console.log(keywordlist)
    this.setData({
      keywordlist: keywordlist
    })
  },
  //获得关键字
  getKeyWord(e){
    var keyword = e.detail.value;
    var status;
    if (keyword == ''){
      this.setData({
        goodsStart: 1,
        endFlage: 1,
        end: 999,
        goodsList: []
      })
      status = 1;
    }else{
      status = 2;
    }
    this.setData({
      keyword: keyword,
      status: status
    })
    console.log(this.data.keyword)
  },
  serach(){
    var keyword = this.data.keyword;
    if (keyword == ''){
      wx.showToast({
        title: '搜索内容不能为空',
        icon:'none'
      })
      return false;
    }


    //缓存搜索历史
    this.saveHistoryword(keyword);
    //发起请求

    this.setData({
      keyword: keyword,
      status : 3
    })
    this.getGoodsListParameter();
  },
  saveHistoryword(keyword){
    var oldkeyWordlist = wx.getStorageSync('keywordlist');
    var newKeyWordList = []
    if (typeof oldkeyWordlist == 'undefined' || oldkeyWordlist == '') {
      console.log(222);
      newKeyWordList.unshift(keyword)
      wx.setStorageSync('keywordlist', newKeyWordList)
      this.setData({
        keywordlist: newKeyWordList
      })
    } else {
      //检查是否有已有
      for (let i = 0; i < oldkeyWordlist.length; i++) {
        console.log(oldkeyWordlist[i])
        var index = oldkeyWordlist.indexOf(keyword);
        if (index > -1) {
          oldkeyWordlist.splice(index, 1);
        }
      }

      if (oldkeyWordlist.length < 10) {
        oldkeyWordlist.unshift(keyword)
        wx.setStorageSync('keywordlist', oldkeyWordlist)
      } else {
        oldkeyWordlist.pop();
        oldkeyWordlist.unshift(keyword);
        wx.setStorageSync('keywordlist', oldkeyWordlist)
      }
      this.setData({
        keywordlist: oldkeyWordlist
      })
    }
  },
  //点击热词
  clickhotword(e){
    var index = e.currentTarget.dataset.index;
    var hotlist = this.data.hotlist;
    for (let i = 0; i < hotlist.length; i++) {
      if (index == i) {
        console.log(hotlist[i])
        var keyword = hotlist[i];
      }
    }
    this.saveHistoryword(keyword);
    this.setData({
      keyword: keyword,
      status: 3
    })
    this.getGoodsListParameter();
  },
  //点击历史
  clickhisword(e) {
    var index = e.currentTarget.dataset.index;
    var hotlist = this.data.keywordlist;
    for (let i = 0; i < hotlist.length; i++) {
      if (index == i) {
        console.log(hotlist[i])
        var keyword = hotlist[i];
      }
    }
    this.setData({
      keyword: keyword,
      status: 3
    })
    this.getGoodsListParameter();
  },
  // 搜索相关
  setSorting() {
    var sortList = [
      { title: "综合排序", select: 0, kind: "comprehensive" },
      { title: "按价格", select: 1, kind: "vipPrice" },
      { title: "按销量", select: 1, kind: "prodcutsellamount" }
    ];
    this.setData({
      sorting: sortList,
      sortKind: sortList[0].kind
    })
  },
  tapStor(e) {
    var index = e.currentTarget.dataset.index;
    var sorting = this.data.sorting;
    for (let i = 0; i < sorting.length; i++) {
      sorting[i].select = 1;
      if (index == i) {
        sorting[i].select = 0;
        this.setData({
          sortKind: sorting[i].kind
        })
      }
    }
    this.setData({
      goodsStart: 1,
      endFlage: 1,
      end: 999,
      sorting: sorting
    })
    console.log(this.data.sortKind)
    if (this.data.sortKind == 'vipPrice') {
      if (this.data.paixu) {
        this.setData({
          paixu: false
        })
      } else {
        this.setData({
          paixu: true
        })
      }
    } else {
      this.setData({
        paixu: true
      })
    }
    console.log(this.data.sortKind, this.data.paixu)
    this.goTopFun();
    this.getGoodsListParameter();
  },
  goTopFun: function (e) {
    var _top = this.data.scrollTop;
    if (_top == 1) {
      _top = 0;
    } else {
      _top = 1;
    }
    this.setData({
      'scrollTop': _top
    });
  },
  getmore() {
    var start = this.data.goodsStart;
    var newStart = start + 1;
    this.setData({
      goodsStart: newStart
    })
    console.log(newStart)
    this.getGoodsListParameter();
  },
  //获得参数
  getGoodsListParameter() {
    console.log(this.data.sortKind)
    var body = {};
    var queryList = [
      "comblex_search:" + this.data.keyword,
      "shopId:" + wx.getStorageSync('supermarket_id')
    ];
    var sortList = [
      this.data.sortKind + ":" + this.data.paixu
    ];
    body.queryList = queryList;
    body.rows = this.data.rows;
    body.sortList = sortList;
    body.start = this.data.goodsStart;
    console.log(2222222222222222)
    console.log(body)
    this.getGoodsList(body)
  },
  getGoodsList(body) {
    var url = getApp().data.url + 'solr/product/productQuery';
    wx.showLoading({
      title: '',
      icon: "loading"
    })
    if (this.data.goodsStart < this.data.end) {
      getApp().wxAjax(this.splitGoodsList, url, body);
    } else {
      wx.hideLoading({});
      if (this.data.endFlage == 1) {
        this.setData({
          endFlage: 2
        })
        wx.showToast({
          title: '全部加载完成',
          icon: 'none'
        })
      }
    }

  },
  splitGoodsList(res) {
    wx.hideLoading({});
    var resObj = res.data;
    if (resObj.header.res_code == 0) {
      var goodsListObj = resObj.body;
      var goodsListArr;
      //无分页时直接取
      if (this.data.goodsStart == 1) {
        goodsListArr = resObj.body.data;
      } else {
        goodsListArr = this.data.goodsList;
        goodsListArr.push.apply(goodsListArr, resObj.body.data);
      }
      this.setData({
        goodsList: goodsListArr,
        end: goodsListObj.pages
      })
      console.log(goodsListObj)
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
  },
  toGoodesDetails(e) {
    var skuid = e.currentTarget.dataset.skuid;
    wx.navigateTo({
      url: '../pDetails/pDetails?skuid=' + skuid
    })
  },
  addGoods(e) {
    var skuid = e.currentTarget.dataset.skuid;
    this.setData({
      sku: skuid
    })
    getApp().addPro(this);
  },
})